package com.niu.controller;

import com.lanyuan.annotation.SystemLog;
import com.lanyuan.controller.index.BaseController;
import com.lanyuan.entity.RoleFormMap;
import com.lanyuan.entity.UserFormMap;
import com.lanyuan.entity.UserGroupsFormMap;
import com.lanyuan.exception.SystemException;
import com.lanyuan.plugin.PageView;
import com.lanyuan.util.Common;

import com.lanyuan.util.PasswordHelper;
import com.niu.entity.CurriculumFormMap;
import com.niu.entity.DiscussFormMap;
import com.niu.entity.ProblemFromMap;
import com.niu.mapper.CurriculumMapper;
import com.niu.mapper.DiscussMapper;
import com.niu.mapper.ProblemMapper;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.inject.Inject;

@Controller
@RequestMapping("/curr")
public class CurriculumController extends BaseController {

    //启用spring4.0注解方式
    @Inject
    private CurriculumMapper     curriculumMapper;

    @Inject
    private DiscussMapper discussMapper;

    @Inject
    private ProblemMapper problemMapper;

    /**
     * 答案列表
     * @return
     */
    @RequestMapping(value = "dis", method = RequestMethod.GET, produces = "text/html; charset=utf-8")
    public String dis(){

        return Common.BACKGROUND_PATH + "/niu/curr/dis";
    }

    @ResponseBody
    @RequestMapping("disByPage")
    public PageView disByPage(String pageNow,
                              String pageSize) throws Exception {
        DiscussFormMap discussFormMap = getFormMap(DiscussFormMap.class);
        try {
            discussFormMap=toFormMap(discussFormMap, pageNow, pageSize,discussFormMap.getStr("orderby"));
            pageView.setRecords(discussMapper.findByPage(discussFormMap));
        }catch (Exception e){
            e.printStackTrace();
        }

        return pageView;
    }


    /**
     * 问题列表
     * @return
     */
    @RequestMapping(value = "pro", method = RequestMethod.GET, produces = "text/html; charset=utf-8")
    public String pro(){

        return Common.BACKGROUND_PATH + "/niu/curr/pro";
    }

    @ResponseBody
    @RequestMapping("proByPage")
    public PageView proByPage(String pageNow,
                               String pageSize) throws Exception {
        ProblemFromMap problemFromMap = getFormMap(ProblemFromMap.class);
        try {
            problemFromMap=toFormMap(problemFromMap, pageNow, pageSize,problemFromMap.getStr("orderby"));
            pageView.setRecords(problemMapper.findByPage(problemFromMap));
        }catch (Exception e){
            e.printStackTrace();
        }

        return pageView;
    }





    @RequestMapping(value = "list", method = RequestMethod.GET, produces = "text/html; charset=utf-8")
    public String index(){

        return Common.BACKGROUND_PATH + "/niu/curr/list";
    }

    @ResponseBody
    @RequestMapping("findByPage")
    public PageView findByPage(String pageNow,
                               String pageSize) throws Exception {
        CurriculumFormMap curriculum = getFormMap(CurriculumFormMap.class);
        try {
            curriculum=toFormMap(curriculum, pageNow, pageSize,curriculum.getStr("orderby"));
            pageView.setRecords(curriculumMapper.findByPage(curriculum));
        }catch (Exception e){
            e.printStackTrace();
        }

        return pageView;
    }

    @RequestMapping("addUI")
    public String addUI(Model model) throws Exception {
        return Common.BACKGROUND_PATH + "/niu/curr/add";
    }


    @ResponseBody
    @RequestMapping("addEntity")
    @Transactional(readOnly=false)//需要事务操作必须加入此注解
    @SystemLog(module="课程管理",methods="添加课程")//凡需要处理业务逻辑的.都需要记录操作日志
    public String addEntity() throws Exception {
        CurriculumFormMap curriculumFormMap = getFormMap(CurriculumFormMap.class);
        curriculumMapper.addEntity(curriculumFormMap);
        return "success";
    }

    @ResponseBody
    @RequestMapping("deleteEntity")
    @Transactional(readOnly=false)//需要事务操作必须加入此注解
    @SystemLog(module="课程管理",methods="课程管理删除课程")//凡需要处理业务逻辑的.都需要记录操作日志
    public String deleteEntity() throws Exception {
        String[] ids = getParaValues("ids");
        for (String id : ids) {
            curriculumMapper.deleteByAttribute("id", id, CurriculumFormMap.class);
        }
        return "success";
    }
}
